<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>编辑</title>
		<link rel="stylesheet" type="text/css" href="../../plugins/layui/css/layui.css" />
		<link href="../../kitAdmin/css/app.css" rel="stylesheet" type="text/css" />
		<link rel="stylesheet" type="text/css" href="../../plugins/select2/dist/css/select2.min.css"/>
		<style type="text/css">
			/* .select2-container{
				display:none;
			} */
			
			.layui-unselect.layui-form-select {
			    display: none;
			}
		</style>
	</head>

	<body>
		<div class='kit-table'>
			<div style='margin:10px'>
				
		<form class='layui-form' action='' lay-filter='form-edit'>
			<!--<input type='hidden' name='id'  data-link='{:id}' />-->
			
			<div class='layui-inline'>
				<label class='layui-form-label'>负责人</label>
				<select data-link='{:fuzerenId}' id='_fuzerenId' name='fuzerenId' class='layui-input'>
					<option value='请选择' selected="true">请选择</option>
				</select>
			</div>
			<div class='layui-inline'>
				<label class='layui-form-label'>试听类型</label>
				<div class='layui-input-block'>
					<input type="radio" data-link='{:trylistenType}' id="_trylistenType0" name="trylistenType" value="新试听" title="新试听"/>
					<input type="radio" data-link='{:trylistenType}' id="_trylistenType1" name="trylistenType" value="二次试听" title="二次试听"/>
				</div>
			</div>
			<br>
			<div class='layui-inline'>
				<label class='layui-form-label'>课程</label>
				<select lay-filter="_course" data-link='{:course}' id='_course' name='course' class='layui-input'>
					<option value='请选择' selected="true">请选择</option>
				</select>
			</div>
			<br>
			<div class='layui-inline'>
				<label class='layui-form-label'>课程等级</label>
				<select lay-filter="_courseLevel" data-link='{:courseLevel}' id='_courseLevel' name='courseLevel' class='layui-input'>
					<option value='请选择' selected="true">请选择</option>
				</select>
			</div>
			<br>
			<div class='layui-inline'>
				<label class="layui-form-label">试听时间</label>
			    <div class="layui-input-inline" style="margin-right: 0px; width: 146px;">
			      	<input type='text' placeholder=''data-link='{:auditionTime}' id='_auditionTime' name='auditionTime' class='layui-input'>
				</div>
			    <span class="input-group-btn">
			        <a style="height: 38px;line-height:38px;" class="layui-btn layui-btn-sm" type="button" onclick="setAudioTime()">添加</a>
		      	</span>
			</div>
			<br>
			<div class='layui-inline'>
				<label class="layui-form-label">预留时间</label>
			    <div class="layui-input-inline" style="margin-right: 0px; width: 146px;">
			      	<input type='text' placeholder=''data-link='{:preTime}' id='_preTime' name='preTime' class='layui-input'>
				</div>
			    <span class="input-group-btn">
			        <a style="height: 38px;line-height:38px;" class="layui-btn layui-btn-sm" type="button" onclick="setRetentionTime()">添加</a>
		      	</span>
                <!--<input type='text' placeholder=''data-link='{:teacherId}' id='_teacherId' name='teacherId' class='layui-input' >-->
            </div>
		</form>
			</div>
		</div>
		<script src="../../common/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../static/js/base.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../plugins/jsviews/jsviews.js" type="text/javascript"></script>
		<script src="../../plugins/layui/layui.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../plugins/select2/dist/js/select2.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
		
		var uid = getQueryVariable('uniqueId');//获取url中的数据
        var teacherid=getQueryVariable('techerid');
        var studentid=getQueryVariable('studentid');
        var courseId=getQueryVariable('course');

        var coursetemp = "1";
		var entity;

		// var callback_save=function(){
		// 	console.log($('.layui-form').serialize());
		// 	return $('.layui-form').serialize();
		// }
        var callback_save = function () {
            var auditionTime=$("#auditionTime").val();
            if(auditionTime==""){
                alert("请选择试听时间");
                return;
            }
            var adds = [], retentions = [], dels = [];
            var objs = [];
            var news = $('input:checkbox[name=wait]:checked').each(function (i) {
                var obj = {};
                obj.teacherid = teacherid;
                obj.checkid = $(this).attr('timeid') + '_' + $(this).attr('weekid');
                obj.week = $(this).attr('weekid');
                obj.timeid = $(this).attr('timeid');
                obj.useMsg = '可用时段';
                obj.retention = '';
                adds.push(obj);
            });

            $('input:checkbox[name=old]:not(:checked)').each(function(i) {
                var obj = {};
                obj.teacherid = teacherid;
                obj.checkid = $(this).attr('id');
                obj.week = $(this).attr('weekid');
                obj.timeid = $(this).attr('timeid');

                obj.status = "试听";
                obj.useMsg = studentName + obj.status;
                obj.retention = 'false';
                obj.date = $("#auditionTime").val();
                obj.studentid = studentid;
                obj.sdStatus = 'delete';
                objs.push(obj);
            });

            var index=0;
            $('input:checkbox[name=old]:checked').each(function (i) {
                index ++;
                var obj = {};
                obj.teacherid = teacherid;
                obj.checkid = $(this).attr('id');
                obj.week = $(this).attr('weekid');
                obj.timeid = $(this).attr('timeid');

                obj.status = "试听";
                obj.useMsg = studentName + "试听";
                obj.retention = 'false';
                obj.date = $("#auditionTime").val();
                obj.studentid = studentid;
                obj.sdStatus = 'add';
                objs.push(obj);
            });
            console.log(objs);
            var datas = [teacherid, JSON.stringify(objs),auditionTime];

            return datas;
//	return $('.layui-form').serialize();
        }

		function bindFormData(){
			if(uid){
				$.ajax({
					url:baseUrl+'/trylistenmanages/getByUid/'+uid,
					type:'get',
					success:function(data){
						entity=data.data;
						console.log('// 2018-10-5 chenbin: ',entity);

                        if (entity.trylistenType != null) {
                            if (entity.trylistenType == '新试听') {
								$("#_trylistenType0").attr("checked", true);
                            } else {
                                $("#_trylistenType1").attr("checked", true);
                            }
                        }

// 						bindEnglishAbility();
// 						bindLessonLevelSelect2();
// 						$.link(true, ".kit-table", entity);
// 						$.link(true, "#_fuzerenId", entity);
						if (entity.courseName != null) {
							debugger;
							$.link(true, "#_course", entity);
						}
						if (entity.course != null) {
							debugger;
							$.link(true, "#_courseLevel", entity);
						}
						if (entity.fuzerenId != null) {
							$.link(true, "#_fuzerenId", entity);
                            $("#_fuzerenId").attr("disabled", "disabled")//将input元素设置为readonly
                        }
                        if (entity.auditionTime != null) {
                            $.link(true, "#_auditionTime", entity);
                        }
                        if(entity.preTime !=null){
                            $.link(true, "#_preTime", entity);
						}

                        if (entity.nowState != null && entity.nowState == '已完成') {
                            $('input').attr("readonly", "readonly")//将input元素设置为readonly
                            $('select').attr("disabled", "disabled")//将input元素设置为readonly
                        }
						$('select').select2().change();
					},
					error:function(){
						parent.layer.alert('系统或网络错误！');
					}
				});
			}
		}
			
			$(document).ready(function() {
				bindFuzerenSelect2();
				bindLessonSelect2();
				bindLessonLevelSelect2();
                bindTeacherSelect2();
                bindFormData();

                $('select').select2().change();

                layui.use(['form'], function(){
					var form = layui.form;
				});
				//select2通过模板绑定值以后不会自动刷新，需手动刷新一下；
				//注意，layui的select和select2用的不是一个控件，如果使用layui的select下面的这句可以注释掉
				//如果是select2，这下面的需要保留
				/* $('select').select2().change();
				
				$('#_course').on("select2:select",function(){
					$('#_courseLevel').empty();
					$.ajax({
						url : "/levels/select2/" + $(this).val(),
						async : false,
						dataType : 'json',
						success : function(result) {
							
							$('#_courseLevel').select2({
								data : result
							});
							
						}
					});
				}); */
			});
			
		function bindLessonSelect2() {
			var courseData = [];
			$.ajax({
				type : "get",
				url :baseUrl+ '/lessons/select2',
				async : false,
				success : function(data) {
					//console.log(data);
					courseData = data;
					courseData.unshift({
						id : '请选择',
						text : '请选择'
					});
				}
			});
			
			$('#_course').select2({
				data : courseData
			});
		}

		function bindLessonLevelSelect2(data) {
			//console.log(entity);
			var courseData = [];
			$.ajax({
				type : "get",
				// url :baseUrl+ '/levels/select2/'+ coursetemp,
				url :baseUrl+ '/levels/select2/'+ courseId,
				async : false,
				success : function(data) {
					courseData = data;
					courseData.unshift({
						id : '请选择',
						text : '请选择'
					});
				}
			});
			
			$('#_courseLevel').select2({
				data : courseData
			});
		}

		// 2018-9-27 chenbin: 初始化负责人下拉框
		function bindFuzerenSelect2() {
			var fuzerenSet = [];
			$.ajax({
				type : "get",
				url : baseUrl+'/admins/select2',
				async : false,
				success : function(data) {
					fuzerenSet = data;
					fuzerenSet.unshift({
						id : '请选择',
						text : '请选择'
					});
				}
			});
			$('#_fuzerenId').select2({
				data : fuzerenSet
			});
		}

        function bindTeacherSelect2() {
            var teacherData = [];
            $.ajax({
                type : "get",
                url :baseUrl+ '/teachers/select2',
                async : false,
                success : function(data) {
                    //console.log(data);
                    teacherData = data;
                    teacherData.unshift({
                        id : '请选择',
                        text : '请选择'
                    });
                }
            });
            $('#_teacherId').select2({
                data : teacherData
            });
            $('#_teacherId').select2().trigger('change');
        }
			
			function setAudioTime(){
                // var teacherid=$("#_teacherId").val();
                // if(teacherid=="请选择"){
					// alert("请选择老师");
					// return;
                // }
				parent.layer.open({
					id:teacherid+'info',
					maxmin: true,
					type: 2,
					area: modalArea,
                    content: './views/Period_Used.html?techerid='+teacherid+'&studentid='+studentid+'&studentName='+encodeURI(entity.enName),
					btn: ['保存', '取消'],
					yes: function(index, layero) {
                        var data_check = $(layero).find("iframe")[0].contentWindow.callback_check();
                        // if(data_check>1){
                        //     alert("试听时间只能选择一个时间段，请重新选择！")
                        //     return false;
                        // }
						var data = $(layero).find("iframe")[0].contentWindow.callback_save();
						console.log(layero);
						$.ajax({
							url:baseUrl+ '/teacherPeriods/periodUsed/save/'+data[0]+'&'+data[2],
							type: 'POST',
							data: data[1],//,teacherId:'1245745'
							dataType:"json",      
			           		contentType:"application/json",
							success: function(data) {
                                $("#_auditionTime").val(data.data);
                                parent.layer.close(index);
							}
						});
					},
					btn2: function(index, layero) {
						//按钮【按钮二】的回调
						console.log(layero);
						//return false 开启该代码可禁止点击该按钮关闭
					}
				});
			}

			function setRetentionTime(){
				var teacherid=$("#_teacherId").val();
                if(teacherid=="请选择"){
					alert("请选择老师");
					return;
				}
				console.log(uid);
				parent.layer.open({
					id:teacherid+'info',
					maxmin: true,
					type: 2,
					area: modalArea,
                    content: './views/Period_Formal.html?techerid='+entity.teacherId+'&studentid='+entity.studentId+'&studentName='+encodeURI(entity.enName),
					btn: ['保存', '取消'],
					yes: function(index, layero) {
						var data = $(layero).find("iframe")[0].contentWindow.callback_save();
						console.log(layero);
						$.ajax({
                            url:baseUrl+ '/teacherPeriods/periodUsedFormal/save/'+data[0]+'&'+data[2],
							type: 'POST',
							data: data[1],//,teacherId:'1245745'
							dataType:"json",      
			           		contentType:"application/json",
							success: function(data) {
								/* alert(data.data); */
                                $("#_preTime").val(data.data);
                                parent.layer.close(index);							}
						});
					},
					btn2: function(index, layero) {
						//按钮【按钮二】的回调
						console.log(layero);
						//return false 开启该代码可禁止点击该按钮关闭
					}
				});
			}

        var callback_save2=function(){
            var msg = $('.layui-form').serialize();
            msg = msg + "&teacherName="+$('#_teacherId option:selected').text();
            msg = msg + "&fuzerenName="+$('#_fuzerenId option:selected').text();
            msg = msg + "&courseName="+$('#_cource option:selected').text();
            msg = msg + "&id="+uid;
            return msg
        }
		</script>
	</body>

</html>
